// 网络状态检测工具

/**
 * 监听网络状态变化
 * @param {function} onChange 网络状态变化回调，参数为{isConnected, networkType}
 */
function listenNetworkStatus(onChange) {
  wx.onNetworkStatusChange(function(res) {
    if (typeof onChange === 'function') {
      onChange({ isConnected: res.isConnected, networkType: res.networkType });
    }
  });
}

/**
 * 获取当前网络状态
 * @returns Promise<{isConnected, networkType}>
 */
function getNetworkStatus() {
  return new Promise((resolve) => {
    wx.getNetworkType({
      success: function(res) {
        resolve({ isConnected: res.networkType !== 'none', networkType: res.networkType });
      },
      fail: function() {
        resolve({ isConnected: false, networkType: 'none' });
      }
    });
  });
}

module.exports = { listenNetworkStatus, getNetworkStatus }; 